<template>
  <div>
    <!--  -->
    <el-row>
      <el-col :span="8">
        <div class="box_size random_box" @click="randomPaperKey = true">随机练习</div>
      </el-col>
      <el-col :span="8">
        <div class="box_size paper_center_box" @click="paperCenter">试卷中心</div>
      </el-col>
      <el-col :span="8">
        <div class="box_size exam_box" @click="examAndTogether">统一考试</div>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <!-- 最近试卷 -->
    <el-row>
      <el-col :span="7" v-for="(practice, index) in recentPractice" :key="index" :offset="index % 3 == 0 ? 0 : 1">
        <div class="paper_box">
          <el-card @click.native="showPracticeInfo(practice)" class="box-card" shadow="hover">
            <!-- <div slot="header" class="clearfix"> -->
              <el-descriptions :title="practice.paperName" :column="1">
                <!-- <el-descriptions-item label="试卷类型">{{ parsePaperType(practice.paperType) }}</el-descriptions-item> -->
                <el-descriptions-item label="试卷编号">{{ practice.paperId }}</el-descriptions-item>
                <el-descriptions-item label="试卷得分">{{ practice.realScore }}</el-descriptions-item>
                <el-descriptions-item label="作答时间">{{ practice.createTime }}</el-descriptions-item>
                <!-- <el-descriptions-item label="出题人">{{ practice.accountName }}</el-descriptions-item> -->
              </el-descriptions>
          </el-card>
        </div>
      </el-col>
    </el-row>
    <!-- 随机练习 -->
    <el-drawer
      title="生成试卷"
      modal
      :visible.sync="randomPaperKey">
      <el-form :model="randomPaper" label-width="80px">
        <el-form-item label="试卷名称">
          <el-input v-model="randomPaper.paperName"></el-input>
        </el-form-item>
        <el-form-item label="试卷描述">
          <el-input v-model="randomPaper.paperDesc"></el-input>
        </el-form-item>
        <el-form-item label="选择题库">
          <el-input v-model="randomPaper.bankName">
            <el-button @click="selectBankKey = true" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="试卷模板">
          <el-input v-model="randomPaper.templateName">
            <el-button @click="selectTemplateKey = true" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="generatePaper">生成试卷</el-button>
          <el-button @click="randomPaperKey = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <el-dialog
      title="近期考试"
      :visible.sync="dialogVisible"
      width="40%">
      <el-button v-for="(exam, index) in recentExam" :key="index" type="text" @click="examAndNow(exam)">{{ exam.paperName }}</el-button>
    </el-dialog>
    <el-dialog
      title="选择模板"
      :visible.sync="selectTemplateKey"
      width="70%">
      <paper-template @selectTemplate="callbackSelected"></paper-template>
    </el-dialog>
    <el-dialog
      title="选择题库"
      :visible.sync="selectBankKey"
      width="60%">
      <paper-bank @selectBank="selectBankHandle"></paper-bank>
    </el-dialog>
    <el-dialog
      title="前往考试"
      :visible.sync="toPracticeKey"
      width="40%">
      <el-button @click="examAndNow(newPaper.paperId)">{{ newPaper.paperName }}</el-button>
    </el-dialog>
    <el-dialog
      title="练习详情"
      :visible.sync="practiceInfoWindowsKey"
      width="70%">
      <practice-info :practiceInfo="currentPracticeInfo"></practice-info>
    </el-dialog>
  </div>
</template>

<script>
import paperTemplate from '@/components/SelectedPaperTemplate.vue'
import paperBank from '@/components/PaperBank.vue'
import practiceInfo from '@/views/PracticeInfo'
import { getConfig, parseConfig, generatePaperSystem, generatePracticeSystem, queryRecentPractice, queryPracticeInfo } from '@/api/system/system.js'
export default {
  components: {
    paperTemplate,
    paperBank,
    practiceInfo
  },
  data() {
    return {
      dialogVisible: false,
      randomPaperKey: false,
      selectTemplateKey: false,
      practiceInfoWindowsKey: false,
      toPracticeKey: false,
      selectBankKey: false,
      newPaper: {},
      paperType: [],
      currentPracticeInfo: {},
      randomPaper: {
        paperName: '',
        paperDesc: '',
        templateId: '',
        templateName: '',
        bankId: '',
        bankName: ''
      },
      recentExam: [
        {
          paperName: '文化常识第一次模拟考'
        },
        {
          paperName: '职业技能基础测试'
        }
      ],
      recentPractice: []
    }
  },
  async mounted() {
    const { data: paperType} = await getConfig({ codeType: 'PAPER_TYPE', codeValue: ''})
    if (paperType.code === 200) {
      this.paperType = paperType.data.cache
    }
    const { data: recentPractice } = await queryRecentPractice(10)
    if (recentPractice.code === 200) {
      this.recentPractice = recentPractice.data.papers
    }
  },
  computed: {
    parsePaperType() {
      return function(type) {
        return parseConfig(this.paperType, type)
      }
    }
  },
  methods: {
    async generatePaper() {
      // 生成试卷，使用vuex传递试卷
      const { data: res } = await generatePaperSystem(this.randomPaper)
      this.newPaper = res.data.paper
      this.toPracticeKey = true
    },
    paperCenter() {
      this.$router.push('/paperCenter')
    },
    selectBankHandle(row) {
      this.randomPaper.bankId = row.bankId
      this.randomPaper.bankName = row.bankName
      this.selectBankKey = false
    },
    examAndTogether() {
      // 检查最近又无考试，有展示考试列表，没有这直接返回
      if (this.recentExam.length > 0) {
        // 展示
        this.dialogVisible = true
      } else {
        return this.$message.success('近期没有考试')
      }
    },
    async showPracticeInfo(practice) {
      const { data: res } = await queryPracticeInfo(practice.paperInstId)
      if (res.code === 200) {
        this.currentPracticeInfo = res.data.paper
        this.practiceInfoWindowsKey = true
      }
    },
    async examAndNow(paperId) {
      // 使用vuex传递试卷
      const {data: res} = await generatePracticeSystem(paperId)
      this.$store.dispatch('current_practice', JSON.stringify(res.data.practice))
      this.$router.push('/paperDetails')
    },
    callbackSelected(row) {
      this.randomPaper.templateId = row.templateId
      this.randomPaper.templateName = row.templateName
      this.selectTemplateKey = false
    }
  }
}
</script>

<style lang="css" scoped>
.box_size {
  width: 240px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  border-radius: 10px;
  color: white;
}
.box_size:hover {
  cursor: pointer;
}
.random_box {
  background-color: blue;
}
.paper_center_box {
  background-color: orange;
}
.exam_box {
  background-color: green;
}
.paper_box {
  /* width: 320px; */
  border-radius: 10px;
  margin-bottom: 25px;
}
.el-button {
  outline: none;
}
</style>